package com.cat.binaryTree;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/linked-list-components/description/
 * @create 2025/9/9 20:27
 * @since JDK17
 */

public class Solution05 {
    public int numComponents(ListNode head, int[] nums) {
        Set<Integer> set = new HashSet<>();
        for (int num : nums) {
            set.add(num);
        }
        int ans = 0;
        for (var p = head; p != null; p = p.next) {
            if (set.contains(p.val)) {
                ans++;
            }
            while (p != null) {
                if (!set.contains(p.val)) {
                    break;
                }
                p = p.next;
            }
        }

        return ans;
    }
}
